const loadGruntTasks = require('load-grunt-tasks')

const DIST_OUTPUT = 'dist';
const TEMP_OUTPUT = '.temp'
const input = (path = '') => 'src' + path

const data = {
  menus: [
    {
      name: 'Home',
      icon: 'aperture',
      link: 'index.html'
    },
    {
      name: 'Features',
      link: 'features.html'
    },
    {
      name: 'About',
      link: 'about.html'
    },
    {
      name: 'Contact',
      link: '#',
      children: [
        {
          name: 'Twitter',
          link: 'https://twitter.com/w_zce'
        },
        {
          name: 'About',
          link: 'https://weibo.com/zceme'
        },
        {
          name: 'divider'
        },
        {
          name: 'About',
          link: 'https://github.com/zce'
        }
      ]
    }
  ],
  pkg: require('./package.json'),
  date: new Date()
}

module.exports = grunt => {
  loadGruntTasks(grunt);
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    babel: {
      options: {
        presets: ['@babel/preset-env']
      },
      main: {
        files: [{
          expand:true,
          src: input('/**/*.js'),
          dest: TEMP_OUTPUT
        }]
      }
    },
    sass: {
      dist: {
        options: {
          style: 'expanded'
        },
        files: [{
          expand: true,
          src: input('/**/*.scss'),
          dest: TEMP_OUTPUT,
          ext: '.css'
        }]
      }
    },
    browserSync: {
      bsFiles: [TEMP_OUTPUT, 'src', 'public'],
      options: {
        server: {
          watchTask: true,
          baseDir: TEMP_OUTPUT
        }
      }
    },
    clean: [TEMP_OUTPUT, DIST_OUTPUT],
    compile: {
      clean: {},
      script: {},
      style: {},
      page: {}
    },
    web_swig: {
      options: {
        swigOptions:{
          cache: false
        },
      },
      files: {
        expand:true,
        src: input('/**/*.html'),
        dest: TEMP_OUTPUT
      }
    },
    watch: {
      script: {
        files: [input('/**/*.js')],
      },
      style: {
        files: [input('/**/*.scss')],
      },
      page: {
        files: [input('/**/*.swig')],
        tasks: ['swig'],
      },
    }
  })

  grunt.loadNpmTasks('grunt-browser-sync');
  grunt.loadNpmTasks('grunt-web-swig');

  grunt.registerTask('script', ['babel']);
  grunt.registerTask('style', ['sass']);
  grunt.registerTask('page', ['web_swig']);

  grunt.registerMultiTask('compile', function() {
    grunt.task.run(this.target)
  });

  grunt.registerTask('serve', ['compile', 'browserSync', 'watch'])
}
